package com.nuo.backend.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nuo.backend.common.utils.R;
import com.nuo.backend.modules.shiro.service.SysUserTokenService;
import com.nuo.backend.modules.user.po.LoginPO;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuo.backend.common.utils.PageUtils;
import com.nuo.backend.common.utils.Query;

import com.nuo.backend.modules.user.dao.UserDao;
import com.nuo.backend.modules.user.entity.UserEntity;
import com.nuo.backend.modules.user.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private SysUserTokenService sysUserTokenService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R login(LoginPO po) {
        UserEntity user = getOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUserId, po.getUserId()).eq(UserEntity::getPassword, new Sha256Hash(po.getPassword()).toHex()));
        if (user == null) {
            return R.error("账号或密码输入有误...");
        }
        return sysUserTokenService.createToken(user.getId(), 1);
    }

    @Override
    public void saveOrUpdateBatch(List<UserEntity> list, Class<UserEntity> clazz) {
        saveOrUpdateBatch(list, "user_id", "userId", clazz);
    }
}